package Generic;

//A simple generic class
//Here, T is a type parameter thar
//will be replaced by a real type
//when an object of type Gen is created.

class Gen<T>{
    T ob; //declare an object of type T

    //Pass the constructor a reference to
    //an object of type T.
    Gen(T o){
        ob=o;
    }

    //Return ob.
    T getOb() {
        return ob;
    }

    //Show type of T
    void showType(){
        System.out.println("Type of T is "+ob.getClass().getName());
    }


}

class Gen2<T,V>  extends Gen<T>{
    V ob2;

    Gen2(T o,V o2){
        super(o);
        ob2=o2;
    }

    V getOb2(){
        return ob2;
    }
}

//Use a wildcard.
class Stats<T extends Number>{
    T[] nums;

    Stats(T[] o){
        nums=o;
    }

    double average() {
        double sum = 0.0;

        for (int i = 0; i < nums.length; i++) {
            sum += nums[i].doubleValue();
        }
            return sum / nums.length;

    }

        boolean sameAvg(Stats<?> ob){
            if(average()==ob.average()) {
                return true;
            } else {
                return false;
            }
        }
    }


//Demonstrate the generic class.
public class GenDemo {
    public static void main(String[] args) {
        Integer inums[]={1,6,3,89,3};
        Double dnums[]={3.2,6.4,123.3,4.0};

        //Stats<Integer> i=new Stats<Integer>(inums);
        //类型推断  简化instance
        Stats<Integer> i=new Stats<Integer>(inums);
        Stats<Double> d=new Stats<Double>(dnums);

        System.out.println(i.average());
        System.out.println(d.average());
        System.out.println(i.sameAvg(d));

    }

}



